package com.servlet.rootoperator;

import com.Impl.BookCRUD;
import com.alibaba.fastjson.JSON;
import com.entity.Book;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AddBookServlet", value = "/AddBookServlet")
public class AddBookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        处理响应头的乱码问题
        response.setContentType("text/html;charset=utf-8");
//        在这里面写添加图书的业务
        String idStr = request.getParameter("bookId");
        System.out.println(idStr);
        int bookId = Integer.parseInt(idStr);
        System.out.println(bookId);
        String bookName = request.getParameter("bookName");
        String category = request.getParameter("category");
        String author = request.getParameter("author");
        String publisher = request.getParameter("publisher");
        String priceStr = request.getParameter("price");
        double price = Double.parseDouble(priceStr);

        BookCRUD bookCRUD = new BookCRUD();
        Book book = new Book(bookId, bookName, category, author, publisher, price);
        int i = bookCRUD.addBook(book);
        System.out.println("插入了"+i+"行数据");
        String s = JSON.toJSONString(book);

        response.getWriter().println(s);

    }
}
